package psd.model.Prolog.Converters;

import java.util.List;

import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;

import psd.model.IConverter;
import psd.model.Prolog.FactGenerationToolbox;
import psd.model.Prolog.PrologConverter;


public class PrologFieldDeclarationConverter extends PrologConverter implements
		IConverter<FieldDeclaration> {

	public PrologFieldDeclarationConverter(FactGenerationToolbox toolbox) {
		super(toolbox);
		// TODO Auto-generated constructor stub
	}

	@Override
	public boolean convert(FieldDeclaration node) {
		List list = node.fragments();
        int size = list.size();
        for(int i = 0; i < size; i++)
        {
            VariableDeclarationFragment fragment = (VariableDeclarationFragment)list.get(i);
            String id = generationToolBox.getIDResolver().getID(fragment);
            String parentId = getParentId(node);
            String type;
            
            
            if(fragment.resolveBinding() == null)
            {
                type = "resolve_failed";
            } else
            {
                type = generationToolBox.getTypeResolver().getTypeTerm(fragment.resolveBinding().getType());
            }
            String name = PrologCommon.quote(fragment.getName().toString());
            String init = generationToolBox.getIDResolver().getID(fragment.getInitializer());
            String args[] = {
                id, parentId, type, name, init, this.getFilePath(node), this.getLineNumber(node)
            };
            generationToolBox.getFactWriter().writeFact("fieldDefT", args);
            int offsetAndLength[] = PrologCommon.excludeComments(node);
            generationToolBox.getFactWriter().writeFact("slT", new String[] {
                id, Integer.toString(offsetAndLength[0]), Integer.toString(offsetAndLength[1]), this.getFilePath(node), this.getLineNumber(node), this.getFilePath(node), this.getLineNumber(node)
            });

            PrologCommon.writeSourceLocationArgumentIdentifier(fragment, fragment.getName(), node.modifiers(), generationToolBox);
            writeModifiers(fragment, node.getModifiers());
        }

        return true;
	}

}
